/*
 * Copyright 2010 Pierre Heinzerling, Uwe Janner, Patrick Wright
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.upp.publinksky.dao;

import com.upp.publinksky.model.Invitation;
import com.upp.publinksky.model.Theme;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import java.util.List;

/**
 */
@SuppressWarnings({"unchecked"})
public class InvitationDAO {
    public static List<Invitation> listAllInvitationsToUser(final String email, final PersistenceManager pm) {
        Query query = pm.newQuery(Invitation.class);
        query.setFilter("toEmail == em");
        query.declareParameters("String em");
        return (List<Invitation>) query.executeWithArray(email);
    }

    public static Invitation findInvitationToThemeForUser(final Theme theme, final String toEmail, final PersistenceManager pm) {
        Query query = pm.newQuery(Invitation.class);
        query.setFilter("theme == thm && toEmail == em");
        query.declareParameters("com.google.appengine.api.datastore.Key thm, String em");

        List<Invitation> existing = (List<Invitation>) query.executeWithArray(theme.getKey(), toEmail);
        return existing.size() == 1 ? existing.get(0) : null;
    }

    public static List<Invitation> listInvitationsToThemeFromUser(final Theme theme, final String fromEmail, final PersistenceManager pm) {
        Query query = pm.newQuery(Invitation.class);
        query.setFilter("theme == thm && fromEmail == em");
        query.declareParameters("com.google.appengine.api.datastore.Key thm, String em");

        return (List<Invitation>) query.executeWithArray(theme.getKey(), fromEmail);
    }

    public static List<Invitation> listInvitationsToTheme(final Theme theme, final PersistenceManager pm) {
        Query query = pm.newQuery(Invitation.class);
        query.setFilter("theme == thm");
        query.declareParameters("com.google.appengine.api.datastore.Key thm");

        return (List<Invitation>) query.execute(theme.getKey());
    }

    public static Invitation insertInvitation(final Theme theme, final String fromEmail, final String toEmail, final PersistenceManager pm) {
        Invitation invite = new Invitation();
        invite.setTheme(theme.getKey());
        invite.setFromEmail(fromEmail);
        invite.setToEmail(toEmail);
        invite.setStatus(Invitation.STATUS_OPEN);
        pm.makePersistent(invite);
        return invite;
    }

    public static void declineInvitation(final Invitation invitation, final PersistenceManager pm) {
        invitation.setStatus(Invitation.STATUS_DECLINED);
        pm.makePersistent(invitation);
    }
}
